﻿using System;
using System.Windows.Input;
using PDFDesign.Properties;
using PDFDesign.Command;
using ICSharpCode.AvalonEdit;
using PDFDesign.Common;
using PDFDesign.Model;
using Newtonsoft.Json;
using MahApps.Metro.Controls.Dialogs;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using PDFDesign.Window;

namespace PDFDesign.ViewModel
{
    public class MockDataViewModel : BaseViewModel
    {
        private TextEditor textEditor;

        public MockDataViewModel(TextEditor textEditor)
        {
            this.textEditor = textEditor;
            textEditor.Text = Utils.JsonFormat(Settings.Default.LastSaveData);
            List<SaveData> dataList = null;
            try
            {
                dataList = JsonConvert.DeserializeObject<List<SaveData>>(Settings.Default.DataList);
            }
            finally
            {
                if (dataList != null) DataList = new ObservableCollection<SaveData>(dataList);
            }
        }

        private SaveData saveData;
        /// <summary>
        /// 当前数据
        /// </summary>
        public SaveData SaveData
        {
            get => saveData;
            set
            {
                saveData = value;
                textEditor.Text = Utils.JsonFormat(saveData?.Data);
                OnPropertyChanged("SaveData");
            }
        }

        private ObservableCollection<SaveData> dataList = new ObservableCollection<SaveData>();
        /// <summary>
        /// 数据列表
        /// </summary>
        public ObservableCollection<SaveData> DataList
        {
            get => dataList;
            set
            {
                dataList = value;
                OnPropertyChanged("DataList");
                Settings.Default.DataList = JsonConvert.SerializeObject(value.ToList());
                Settings.Default.Save();
            }
        }

        /// <summary>
        /// 设置
        /// </summary>
        public ICommand PreviewCommand { get => new AnotherCommand(_ => Preview()); }
        private void Preview()
        {
            object obj = null;
            try
            {
                obj = JsonConvert.DeserializeObject(textEditor.Text);
                if (obj == null)
                {
                    ShowMessage("数据格式错误");
                    return;
                }
            }
            catch (Exception ex)
            {
                ShowMessage($"数据格式错误：{ex.Message}");
                return;
            }
            var data = JsonConvert.SerializeObject(obj);
            Settings.Default.LastSaveData = data;
            Settings.Default.Save();
            DialogResult = true;
        }

        private bool? dialogResult;
        public bool? DialogResult
        {
            get => dialogResult;
            set
            {
                dialogResult = value;
                OnPropertyChanged("DialogResult");
            }
        }

        /// <summary>
        /// 格式化
        /// </summary>
        public ICommand FormatCommand { get => new AnotherCommand(_ => textEditor.Text = Utils.JsonFormat(textEditor.Text)); }
        /// <summary>
        /// 还原修改
        /// </summary>
        public ICommand ReductionCommand { get => new AnotherCommand(_ => textEditor.Text = Settings.Default.LastSaveData); }
        /// <summary>
        /// 添加数据
        /// </summary>
        public ICommand AddDataCommand
        {
            get => new AnotherCommand(_ =>
            {
                var window = new SaveDataWindow();
                var flag = window.ShowDialog();
                if (flag.HasValue && flag.Value)
                {
                    var saveData = window.saveDataViewModel.SaveData;
                    DataList.Add(saveData);
                    SaveData = saveData;
                    DataList = dataList;
                }
            });
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        public ICommand DelDataCommand
        {
            get => new AnotherCommand(_ =>
            {
                if (SaveData == null)
                {
                    ShowMessage("请先选择数据");
                    return;
                }
                DataList.Remove(SaveData);
                DataList = dataList;
                SaveData = null;
            });
        }

        /// <summary>
        /// 提示消息
        /// </summary>
        /// <param name="message"></param>
        private void ShowMessage(string message) => DialogCoordinator.Instance.ShowMessageAsync(this, "温馨提示", message, MessageDialogStyle.Affirmative);
    }
}
